//
//  WKUseRulesCell.m
//  WeKeTang
//
//  Created by ichina on 2017/9/12.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKUseRulesCell.h"
#import "UILabel+Addtiob.h"
#import "WKLoginHandel.h"

@interface WKUseRulesCell ()

@property (nonatomic, copy) NSString *hostdate;

@end

@implementation WKUseRulesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    [self.symbolLabel bottomAlignment];
    [self.priceLabel bottomAlignment];
    self.priceLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(48.0)];
    
    self.hostdate = [WKLoginHandel getLoginDataWithString:@"hostdate"];
    
}
- (void)setModel:(WKDiscountCouponModel *)model{
    
    _model = model;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *hostdate = [dateFormatter dateFromString:self.hostdate];
    NSDate *expireDate = [dateFormatter dateFromString:model.expiretime];
    //比较时间
    NSInteger index = [self compareOneDay:hostdate withAnotherDay:expireDate];
    
    if (index == 1 ) {
        //过期
    
    }else if(index == -1){
        //没过期 可用
        if ([model.usestatus isEqualToString:@"0"]) {
            self.bgImageView.image = [UIImage imageNamed:@"icon_youhuiquan_keyong"];
            self.symbolLabel.textColor = RGBColor(2, 168, 186);
            self.priceLabel.textColor = RGBColor(2, 168, 186);
            self.contentLabel.textColor = [UIColor blackColor];
            self.priceLabel.text = model.money;
            self.timeLabel.text = [NSString stringWithFormat:@"%@到期",model.expiretime];
        //没过期 不可用
        }else if ([model.usestatus isEqualToString:@"1"]){
            self.bgImageView.image = [UIImage imageNamed:@"icon_youhuiquan_yishiyong"];
            self.symbolLabel.textColor = RGBColor(102, 102, 102);
            self.priceLabel.textColor = RGBColor(102, 102, 102);
            self.contentLabel.textColor = RGBColor(102, 102, 102);
            self.priceLabel.text = model.money;
            self.timeLabel.text = [NSString stringWithFormat:@"%@到期",model.expiretime];
        }
        
    }else if(index == 0){
        //相等
        
        
    }
    //可用
    NSLog(@"比较时间 %@----%@",hostdate,expireDate);
    NSLog(@"是否过期%ld",index);
    //不可用
    
}
//比较时间方法
- (NSInteger)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    
    if (result == NSOrderedDescending) {
        //在指定时间前面 过了指定时间 过期
        return 1;
    }
    else if (result == NSOrderedAscending){
        //没过指定时间 没过期
        return -1;
    }
    //刚好时间一样.
    return 0;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
